﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Data.Linq.Mapping;

public class Customceva
{
    //din aspnet_User
    public string UserName { get; set; }
    //din Bookmark
    public decimal Index { get; set; }
    public string Titlu { get; set; }
    public string Link { get; set; }
    public string Owner { get; set; }
    public string Descriere { get; set; }
    public string Taguri { get; set; }
    public System.Nullable<int> Voturi { get; set; }
    //din Comments
    public string Message { get; set; }
}
public partial class AddBookmark : System.Web.UI.Page
{
    protected void Generate(String titlu, String link) {
        WebsitesScreenshot.WebsitesScreenshot _WebsitesScreenshot = new WebsitesScreenshot.WebsitesScreenshot();
        WebsitesScreenshot.WebsitesScreenshot.Result _Result;
        string path = null;
        path = Server.MapPath(".");
        _WebsitesScreenshot.ImageHeight = 160;
        _WebsitesScreenshot.ImageWidth = 160;
        if(link.Contains("http"))
             _Result = _WebsitesScreenshot.CaptureWebpage(link);
        else 
             _Result = _WebsitesScreenshot.CaptureWebpage("http://" + link);

        if (_Result == WebsitesScreenshot.WebsitesScreenshot.Result.Captured)
        {
            _WebsitesScreenshot.SaveImage(path + "\\screens\\" + titlu.Replace('/', '_') + ".jpg");
            Response.Write("");
            Response.Write("<img src="+titlu+">");
        }
        else if (_Result == WebsitesScreenshot.WebsitesScreenshot.Result.Failed)
        {
            Response.Write("");
            Response.Write("Failed");
        }
        else if (_Result == WebsitesScreenshot.WebsitesScreenshot.Result.Timeout)
        {
            Response.Write("");
            Response.Write("Timeout");
        }
        _WebsitesScreenshot.Dispose();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBoxOwner.Text = Page.User.Identity.Name;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Generate(Convert.ToString(TextBoxTitlu.Text), Convert.ToString(TextBoxLink.Text));
            SqlConnection connection = new SqlConnection(
                @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
            );
            connection.Open();

            SqlCommand query = new SqlCommand("INSERT INTO Bookmark (Titlu, Owner, Descriere, Taguri, Link) VALUES (@Titlu, @Owner, @Descriere, @Taguri, @Link)", connection);
            SqlParameter param1 = new SqlParameter("@Titlu", Convert.ToString(TextBoxTitlu.Text));
            SqlParameter param5 = new SqlParameter("@Link", Convert.ToString(TextBoxLink.Text));
            SqlParameter param2 = new SqlParameter("@Owner", null ); 
            DataContext db = new DataContext("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True;");
            Table<aspnet_User> aspnet_Users = db.GetTable<aspnet_User>();
            Decimal URIvar = System.Convert.ToDecimal(Request["id"]);
            IQueryable<Customceva> BookmarkQuery = from s in aspnet_Users
                                                  where s.UserName == Convert.ToString(TextBoxOwner.Text)
                                                   select new Customceva { Owner = s.UserId };
            foreach (Customceva i in BookmarkQuery)
            {
                param2 = new SqlParameter("@Owner", i.Owner );
            }    
            
            SqlParameter param3 = new SqlParameter("@Descriere", Convert.ToString(TextBoxDescriere.Text));
            SqlParameter param4 = new SqlParameter("@Taguri", Convert.ToString(TextBoxTaguri.Text));


            query.Parameters.Add(param1);
            query.Parameters.Add(param2);
            query.Parameters.Add(param3);
            query.Parameters.Add(param4);
            query.Parameters.Add(param5);

            query.ExecuteNonQuery();

            connection.Close();
            Response.Redirect("~/MyBookmarks.aspx");
        
    }
}
